#include "ObjectManager.h"

int ObjectManager::nextPos = 0;

std::map<int,ObjectMessage*> ObjectManager::outgoingMessageMap = std::map<int,ObjectMessage*>();
std::map<int,ObjectMessage*> ObjectManager::incomingMessageMap = std::map<int,ObjectMessage*>();
std::vector<PlayerEnterMessage*>ObjectManager::players = std::vector<PlayerEnterMessage*>();

ObjectManager::ObjectManager(){
}

ObjectManager::~ObjectManager(){
}

ObjectMessage* ObjectManager::createObject()
{
	ObjectMessage* message = new ObjectMessage(nextPos++);
	outgoingMessageMap.insert(std::make_pair(message->id, message));

	return message; 
}

PlayerEnterMessage* createPlayerEnterMessage(std::string name) 
{
	PlayerEnterMessage* playerEnterMessage = new PlayerEnterMessage(name);
	//playerMessageMap.insert(std::make_pair(player->id, player));
	ObjectManager::players.push_back(playerEnterMessage);

	return playerEnterMessage; 
}

PlayerLeaveMessage* createPlayerLeaveMessage(int id)
{
	PlayerLeaveMessage* playerLeaveMessage = new PlayerLeaveMessage(id);
	//playerMessageMap.insert(std::make_pair(player->id, player));

	return playerLeaveMessage; 
}

void ObjectManager::receiveObjectMessage(ObjectMessage *receivedMessage){

	unsigned long id = receivedMessage->id;

	if(incomingMessageMap[id]==NULL)
	{
		ObjectMessage* messageCopy = new ObjectMessage(id);
		incomingMessageMap[id] = messageCopy;
	}

	*incomingMessageMap[id] = *receivedMessage;

}


void ObjectManager::sendObjectMessage(ObjectMessage *changedMessage, boost::asio::ip::udp::endpoint destination)
{
	Network::sendMessage(changedMessage);
}



std::map<int, ObjectMessage*>::const_iterator ObjectManager::getOutgoingMessageMapBegin() 
{
	return outgoingMessageMap.begin();
}

std::map<int, ObjectMessage*>::const_iterator ObjectManager::getOutgoingMessageMapEnd() 
{
	return outgoingMessageMap.end();
}

std::map<int, ObjectMessage*>::const_iterator ObjectManager::getIncomingMessageMapBegin() 
{
	return incomingMessageMap.begin();
}

std::map<int, ObjectMessage*>::const_iterator ObjectManager::getIncomingMessageMapEnd() 
{
	return incomingMessageMap.end();
}
